package Project;
import Hulppakket.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

public class testRenderer implements MouseListener {

	public static void main(String[] args) {
		new testRenderer();
	}

	private int x;
	private int y;
	private JFrame frame;
	private CgPanel panel;

	public testRenderer() {
		try {
			SceneBuilder sceneBuilder = new SceneBuilder();
			Scene scene = sceneBuilder.loadScene("/Users/roel/Downloads/practicum1_linux/XML/example.sdl");
		}
		catch (Exception e) {
			e.printStackTrace();
		}


		panel = new CgPanel();
		panel.addMouseListener(this);
		frame = new JFrame();
		frame.setSize(1024,800);
		frame.getContentPane().add(panel);
		frame.setVisible(true);
	}

	public void mousePressed(MouseEvent e) { }
	public void mouseClicked(MouseEvent e) { timePerformance(); }
	public void mouseEntered(MouseEvent e) { }
	public void mouseExited(MouseEvent e) { }
	public void mouseReleased(MouseEvent e) { }

	public void drawPixels() {
		for (int row=10; row<panel.getHeight()-10; row++) {
			for (int column=10; column<panel.getWidth()-10; column++) {
				if (column>x && column < x+100) {
					panel.drawPixel(column, row, 1,0,0);
				}
				else if (row>y && row < y+100) {
					panel.drawPixel(column, row, 1, 1,0);
				}
				else {
					panel.drawPixel(column, row, 0.5f,0.5f,0.5f);
				}
			}
		}
		x = (x+1)%(panel.getWidth()+1);
		y = (y+1)%(panel.getHeight()+1);
	}

	public void startTest() {
		Point3f eye = new Point3f(-2,2,0);
		Vector3f gaze = new Vector3f(5,0,0);
		Vector3f up = new Vector3f(0,0,1);
		Camera c = new Camera(eye,gaze,up);
		Surface g = buildTestScene();
		Color3f pc = new Color3f();
		Ray r = c.generateRay(1, 1, panel.getWidth(), panel.getHeight());
		Light light = new Light(0.45F,new Point3f(-3,-7,10));
		Light light3 = new Light(0.45F, new Point3f(-3,7,10));
		Shader shader = new Shader();
		shader.setAmbientIntensity(0.1F);
		shader.addLight(light);
		shader.addLight(light3);
		for (int i=1; i<panel.getWidth(); i++) {
			for (int j =1; j<panel.getHeight(); j++)
			{
				r = c.generateRay(i, j, panel.getWidth(), panel.getHeight());
				pc = shader.raycolor(r, 0, 10000, g);
				panel.drawPixel(i, j, pc.x, pc.y, pc.z);
			}

		}
	}

	public Surface buildTestScene() {
		Sphere s = new Sphere(2F,new Point3f(5,0,0),new Color3f(Color.cyan));
		Sphere s2 = new Sphere(1F,new Point3f(5,3,-1), new Color3f(Color.green));
		Group g = new Group();
		g.add(s);
		g.add(s2);
		Triangle t = new Triangle(new Point3f(-10,8,-2),new Point3f(-10,-8,-2), new Point3f(10,8,-2), new Color3f(Color.yellow));
		g.add(t);
		t = new Triangle(new Point3f(-10,-8,-2),new Point3f(10,-8,-2), new Point3f(10,8,-2), new Color3f(Color.yellow));
		g.add(t);
		t = new Triangle(new Point3f(10,8,-2),new Point3f(10,-8,-2), new Point3f(10,-8,10), new Color3f(Color.gray));
		g.add(t);
		t = new Triangle(new Point3f(10,8,10),new Point3f(10,8,-2), new Point3f(10,-8,10), new Color3f(Color.gray));
		g.add(t);
		t = new Triangle(new Point3f(10,8,-2),new Point3f(10,8,10), new Point3f(-10,8,10), new Color3f(Color.blue));
		g.add(t);
		t = new Triangle(new Point3f(-10,8,-2),new Point3f(10,8,-2), new Point3f(-10,8,10), new Color3f(Color.blue));
		g.add(t);
		t = new Triangle(new Point3f(10,-8,-2),new Point3f(-10,-8,10), new Point3f(10,-8,10), new Color3f(Color.pink));
		g.add(t);
		t = new Triangle(new Point3f(-10,-8,10),new Point3f(10,-8,-2), new Point3f(-10,-8,-2), new Color3f(Color.pink));
		g.add(t);
		return g;
	}

	public void timePerformance() {
		long starttime = System.currentTimeMillis();
		for (int i=0; i<1; i++) {
			panel.clear(0,0,0);
			startTest();
			panel.repaint();
			panel.flush();
		}
		long endtime = System.currentTimeMillis();
		double fps = 1000.0/((double)(endtime-starttime)/100.0);
		System.out.println("Fps: " + fps);
		panel.saveImage("image.png");
	}
}
